#include "http_def.h"

static char data_buf[1500];//接收来自缓冲区的数据
void  tcp_webserver_server_conn_close(struct tcp_pcb *pcb,struct http_state *hs)
{
	tcp_arg(pcb, NULL);
	tcp_sent(pcb, NULL);
	tcp_recv(pcb, NULL);
	mem_free(hs);
	tcp_close(pcb);
	tcp_abort(pcb);
	tcp_poll(pcb, NULL,0);
	hs = NULL;
}
void  tcp_webserver_server_data_send(struct tcp_pcb *pcb, struct http_state *hs)//发送数据
{
	err_t err;
	u16_t len;

	if (tcp_sndbuf(pcb) < hs->left)
	{
		len = tcp_sndbuf(pcb);
	}
	else
	{
		len = hs->left;
	}

	err = tcp_write(pcb, hs->file, len, 0);
	if(err == ERR_OK)
	{
		err = tcp_output(pcb);
	}
	if (err == ERR_OK)
	{
		hs->file += len;
		hs->left -= len;
	}
	else
	{
		tcp_webserver_server_conn_close(pcb, hs);
	}
}
err_t tcp_webserver_server_poll(void *arg, struct tcp_pcb *pcb)
{
	struct http_state *hs = arg;
	if (arg == NULL || hs->file == NULL || hs->left == 0)
	{
		tcp_webserver_server_conn_close(pcb, hs);
	}
	else
	{
		tcp_webserver_server_data_send(pcb, hs);
	}
	return ERR_OK;
}
void  tcp_webserver_server_conn_err(void *arg, err_t err)
{
    http_state *hs;
    hs = (http_state *)arg;
	mem_free(hs);
	hs=NULL;
}
err_t tcp_webserver_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    struct pbuf *pp;
    uint16_t length=0;
    char *start=0;
	struct http_state *hs = arg;
	int num=~0;//请求资源的序号
	char temp[4];//获取请求资源的序号
	char temp2[5];//获取请求资源的浏览器类型//IEx:Internet Explorer， NIE:不是IE
	char* referer01;//判断请求页面的来源是否为主框架页面，非opera浏览器使用
	char* referer02;//判断请求页面的来源是否为主框架页面，非opera浏览器使用
 	//char* referer13;//判断请求页面的来源是否为导航页，opera浏览器使用
	char from01[35];//存放比对字符串
	char from02[35];//存放比对字符串
 	//char from13[35];//存放比对字符串
	char *ptemp;//切割出的内容
	char delim[] = " ";//切割字符串	
	if (p != NULL)
	{
		tcp_recved(pcb, p->tot_len);
        pp=p;
        start=data_buf;
        do
        {
            start+=length;
            memcpy(start,pp->payload,pp->len);
            length=pp->len;
            pp=pp->next;
        }
        while(pp!=0);
        start=0;
        length=0;
        pp=0;


		if (hs->file == NULL)
		{
			memcpy(temp,data_buf+7,3);
			memcpy(temp2,data_buf+11,4);
			temp2[4]=0;
            num=atoi(temp);//获取序号
			switch(*(data_buf+5))//h or j or c or i or s or a  etc.    第五位字符用来判断请求的资源类型
			{
				case 'h'://html
					if(strcmp(temp2,"IExx")==0)//来自于IE浏览器
					{
// 						sprintf(from01,"http://%d.%d.%d.%d/",System_Info.Net.ip[0],System_Info.Net.ip[1],System_Info.Net.ip[2],System_Info.Net.ip[3]);
// 						referer01=strstr(data_buf,from01);
// 						//fenglk 20130624 防止点各参数界面弹出登录界面
// 						if(referer01!=NULL)//请求来源正确，返回相应页面
// 						{
							hs->file = (char*)h_Rsc[num].data;
							hs->left = (int)h_Rsc[num].size-1;
// 						}
// 						else//否则返回登录页面
// 						{
// 							hs->file = (char*)h_Rsc[14].data;
// 							hs->left = (int)h_Rsc[14].size;
// 						}
					}
					//ccg 2013.7.2 登录进去后防止弹出登录窗口
					else if(strcmp(temp2,"aspx")==0)//框架调用
					{
// 						sprintf(from01,"http://%d.%d.%d.%d/Index.html",System_Info.Net.ip[0],System_Info.Net.ip[1],System_Info.Net.ip[2],System_Info.Net.ip[3]);
// 						referer01=strstr(data_buf,from01);
// 						if(referer01!=NULL)//请求来源正确，返回相应页面
// 						{
							hs->file = (char*)h_Rsc[num].data;
							hs->left = (int)h_Rsc[num].size-1;
// 						}
// 						else//否则返回登录页面
// 						{
// 							hs->file = (char*)h_Rsc[14].data;
// 							hs->left = (int)h_Rsc[14].size;
// 						}
					}
					else if(strcmp(temp2,"NIEx")==0)//来自于非IE浏览器
					{
// 						sprintf(from13,"http://%d.%d.%d.%d/h_013.aspx",System_Info.Net.ip[0],System_Info.Net.ip[1],System_Info.Net.ip[2],System_Info.Net.ip[3]);
// 						referer13=strstr(data_buf,from13);
// 						if(referer13!=NULL)//请求来源正确，返回相应页面
// 						{
							hs->file = (char*)h_Rsc[num].data;
							hs->left = (int)h_Rsc[num].size-1;
// 						}
// 						else//否则返回登录页面
// 						{
// 							hs->file = (char*)h_Rsc[14].data;
// 							hs->left = (int)h_Rsc[14].size;
// 						}
					}
					else//否则返回登录页面
					{
						hs->file = (char*)h_Rsc[14].data;
						hs->left = (int)h_Rsc[14].size;
					}
				break;
				case 'j'://javascript
					ptemp = strtok(data_buf,delim);
					ptemp = strtok(NULL,delim);
					if(!strcmp(ptemp,"/jquery.flot.min.js"))
					{
						hs->file = (char*)j_Rsc[3].data;
						hs->left = (int)j_Rsc[3].size-1;						
					}
					else if(!strcmp(ptemp,"/jquery.min.js"))
					{
						hs->file = (char*)j_Rsc[4].data;
						hs->left = (int)j_Rsc[4].size-1;							
					}
					else if(!strcmp(ptemp,"/jquery.flot.time.js"))
					{
						hs->file = (char*)j_Rsc[5].data;
						hs->left = (int)j_Rsc[5].size-1;							
					}
					else 
					{
						hs->file = (char*)j_Rsc[num].data;
						hs->left = (int)j_Rsc[num].size-1;						
					}
				break;
				case 'e':
					ptemp = strtok(data_buf,delim);
					ptemp = strtok(NULL,delim);
					if(!strcmp(ptemp,"/excanvas.min.js"))
					{
						hs->file = (char*)j_Rsc[6].data;
						hs->left = (int)j_Rsc[6].size-1;							
					} 
				break;
				case 'c'://css
					hs->file = (char*)c_Rsc[num].data;
					hs->left = (int)c_Rsc[num].size-1;
				break;
				case 'i'://image
					hs->file = (char*)i_Rsc[num].data;
					hs->left = (int)i_Rsc[num].size;
				break;
				case 's'://SetValue()
					s_handler(num,data_buf);
					hs->file = (char*)s_Rsc[0].data;
					hs->left = (int)s_Rsc[0].size;
				break;
				case 'a'://ajax
					a_handler(num,data_buf);
					hs->file = (char*)a_Rsc[0].data;
					hs->left = (int)a_Rsc[0].size;
				break;
				case 'r'://查询历史记录
					r_handler(num,data_buf);
					hs->file = (char*)p_Rsc[0].data;
					hs->left = (int)p_Rsc[0].size;
				break;
				case 'I'://Index.html
					sprintf(from01,"%d.%d.%d.%d",System_Info.Net.ip[0],System_Info.Net.ip[1],System_Info.Net.ip[2],System_Info.Net.ip[3]);
					referer01=strstr(data_buf,from01);
					sprintf(from02,"http://%d.%d.%d.%d/Login.html",System_Info.Net.ip[0],System_Info.Net.ip[1],System_Info.Net.ip[2],System_Info.Net.ip[3]);
					referer02=strstr(data_buf,from02);
					if(loginFlag==0)  //无用户登录
					{
						hs->file = (char*)h_Rsc[14].data;
						hs->left = (int)h_Rsc[14].size;						
					}
					else
					{
						if(loginFlag3==1)		//配置界面
						{
							hs->file = (char*)h_Rsc[20].data;
							hs->left = (int)h_Rsc[20].size;
						}
						else
						{
							if(referer02!=NULL)//确认该页由登录页面请求
							{
								hs->file = (char*)h_Rsc[1].data;
								hs->left = (int)h_Rsc[1].size;
							}
							else if(referer01!=NULL)//确认该页由登录页面请求
							{
								hs->file = (char*)h_Rsc[14].data;
								hs->left = (int)h_Rsc[14].size;
							}
						}
					}
				break;
				case 'R'://运行记录csv
					his_handler(6,data_buf);
					hs->file = (char*)p_Rsc[0].data;
					hs->left = (int)p_Rsc[0].size;
				break;
				case 'E'://事件记录csv
					his_handler(7,data_buf);
					hs->file = (char*)p_Rsc[0].data;
					hs->left = (int)p_Rsc[0].size;
				break;
				case 'F'://故障记录csv
					his_handler(8,data_buf);
					hs->file = (char*)p_Rsc[0].data;
					hs->left = (int)p_Rsc[0].size;
				break;
				case 'L'://Login.html
					loginFlag3=0;
					hs->file = (char*)h_Rsc[0].data;
					hs->left = (int)h_Rsc[0].size;
				break;	
				default://Index.html
					hs->file = (char*)h_Rsc[14].data;
					hs->left = (int)h_Rsc[14].size;
				break;	
			}
			tcp_webserver_server_data_send(pcb, hs);//发送请求资源
			tcp_sent(pcb, tcp_webserver_server_sent);
			pbuf_free(p);		
		}
		else
		{
			pbuf_free(p);
		}
	}
    else
    {
        tcp_close(pcb);
        tcp_abort(pcb);
    }
	
	if (!(err == ERR_OK)) mem_free(hs);

	System_Info.EMS_MONITOR_Web_Http_Com_Count=SIGNAL_FULL;

	return ERR_OK;	   
}
err_t tcp_webserver_server_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{ 
    struct http_state *hs;

    hs = mem_malloc(sizeof(struct http_state));

    if (hs == NULL)
    {
        return ERR_MEM;
    }

    hs->file = NULL;
    hs->left = 0;

    tcp_accepted(pcb);
    tcp_setprio(pcb,TCP_PRIO_MIN);    // Set priority

    tcp_arg(pcb, hs);

	tcp_recv(pcb,tcp_webserver_server_recv);
	tcp_err (pcb,tcp_webserver_server_conn_err);
	tcp_poll(pcb,tcp_webserver_server_poll,20);//twice per second
	return ERR_OK;
}
err_t tcp_webserver_server_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
	struct http_state *hs = arg;

	if (hs->left > 0)
	{
		tcp_webserver_server_data_send(pcb, hs);
	}
	else
	{
		tcp_webserver_server_conn_close(pcb, hs);
	}

	return ERR_OK;
}
